package com.liusand.web.controller;

import com.liusand.web.mapper.CategoryMapper;
import com.liusand.web.service.BlogService;
import com.liusand.web.service.CategoryService;
import com.liusand.xo.common.Result;
import com.liusand.xo.pojo.Category;
import com.liusand.xo.vo.BlogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author LiuSha
 */
@RestController
@RequestMapping("blog")
public class BlogController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("findAll")
    public Result findAll(){
        String categoryId = "%";
        List<BlogVO> list = blogService.findAll(categoryId);
        return Result.success(list);
    }

    @GetMapping("findAllByCategoryId/{categoryId}")
    public Result findAllByCategoryId(@PathVariable String categoryId){
        if(categoryId == null || categoryId.length() == 0){
            return null;
        }
        List<BlogVO> list = blogService.findAll(categoryId);
        return Result.success(list);
    }

    @GetMapping("findCategories")
    public Result findCategories(){
        List<Category> all = categoryService.findAll();
        return Result.success(all);
    }
}
